<template>
  <div class="app-container">
    <el-card class="box-card">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会议主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入会议主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会议室" prop="meettingId">
        <el-input
          v-model="queryParams.meettingId"
          placeholder="请输入会议室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会议状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择会议状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_meet_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="appointmentList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="会议主题" align="center" prop="title" width="130px"/>
      <el-table-column label="会议室名称" align="center" prop="meettingRoom.name" width="150px"/>
      <el-table-column label="发起人" align="center" prop="promoter" />
      <el-table-column label="主持人" align="center" prop="host" />
      <el-table-column label="参会人数" align="center" prop="number" />
      <el-table-column label="会议开始时间" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议时长" align="center" prop="duration">
        <template slot-scope="scope">
          {{ scope.row.duration }}分钟
        </template>
      </el-table-column>
      <el-table-column label="会议状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_meet_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="会议描述" align="center" prop="description" width="130px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:appointment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:appointment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改会议预约信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会议主题" prop="title">
              <el-input v-model="form.title" placeholder="请输入会议主题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发起人" prop="promoter">
              <el-input v-model="form.promoter" placeholder="请输入发起人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主持人" prop="host">
              <el-input v-model="form.host" placeholder="请输入主持人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参会人数" prop="number">
              <el-input-number v-model="form.number" :min="0" :max="1000" label="请输入参会人数"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会议描述" prop="description">
              <el-input type="textarea" :rows="3" v-model="form.description" placeholder="请输入会议描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议日期" prop="startDate" label-width="80px">
              <el-date-picker
                v-model="form.datee"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10" >
            <el-form-item label="会议开始时间" prop="startDate" label-width="100px">
              <el-time-select
                placeholder="起始时间"
                v-model="form.startDate"
                :picker-options="{
                step: '00:20',
                end: '18:30',}">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="会议结束时间" prop="endDate" label-width="100px">
              <el-time-select
                placeholder="结束时间"
                v-model="form.endDate"
                :picker-options="{
                step: '00:20',
                end: '18:30'}">
              </el-time-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="会议状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择会议状态">
            <el-option
              v-for="dict in dict.type.sys_meet_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { listAppointment, getAppointment, delAppointment, updateAppointment } from "@/api/system/appointment";

export default {
  name: "Appointment",
  dicts: ['sys_meet_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议预约表表格数据
      appointmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      startTime:new Date().getHours().toString().padStart(2, '0')+":"+new Date().getMinutes().toString().padStart(2, '0'),
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() <= Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '明天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        meettingId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "会议状态:1未开始，2进行中，3已结束不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会议预约表列表 */
    getList() {
      this.loading = true;
      listAppointment(this.queryParams).then(response => {
        this.appointmentList = response.rows;
        this.appointmentList.forEach(function(n,i){
            n.startDate = n.datee +" "+ n.startDate;
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        title: null,
        meettingId: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppointment(id).then(response => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.title = "修改会议预约信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            console.log("222")
            console.log(this.form)
            updateAppointment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会议预约表编号为"' + ids + '"的数据项？').then(function() {
        return delAppointment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/appointment/export', {
        ...this.queryParams
      }, `appointment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
